import Vue from 'vue'
import VueRouter from 'vue-router'
import store from '@/store'
import Login from '@/views/login'
import Workbeach from '@/components/main/workbeach.vue'
import Layout from '@/views/layout'
import Emp from '@/components/main/emp'
import Category from '@/components/main/category'
import Dish from '@/components/main/dish'
import Meal from '@/components/main/meal'
import Order from '@/components/main/order'
import Datacount from '@/components/main/datacount'

Vue.use(VueRouter)

const router = new VueRouter({
  routes: [
    {
      path: '/login',
      name: 'login',
      component: Login
    },
    { path: '/home', component: Layout }, // 根路径
    {
      path: '/',
      redirect: '/workbeach',
      component: Layout, // 主页
      children: [
        { path: '/workbeach', component: Workbeach },
        { path: '/emp', component: Emp },
        { path: '/category', component: Category },
        { path: '/dish', component: Dish },
        { path: '/meal', component: Meal },
        { path: '/order', component: Order },
        { path: '/datacount', component: Datacount }

      ]
    }
  ]
}
)

const authUrls = ['/workbeach', '/dish', '/meal', 'order']
router.beforeEach((to, from, next) => {
  // 看to.path是否在anthUrls里面出现过
  if (!authUrls.includes(to.path)) {
    // 非权限页面，直接放行
    next()
    // return
  }
  // next('/login')

  // 是权限页面，需要判断token
  /* const token = store.state.user.userInfo.token */
  const token = store.getters.token
  // console.log(token)
  if (token.token === '') {
    next('/login')
  }
  if (token) {
    next()
  } else {
    next('/login')
  }
})
export default router
